const UseModule = {
  state: {
    use: JSON.parse(localStorage.getItem('use')) || [], // 用户数组
  },
  getters: {},
  mutations: {
    addToUse (state, payload) {
      const has = state.use.some((prod) => prod.username === payload.username)
      if (has) {
        // 有
        console.log('用户名以重复')
        alert('用户名以重复')
        sessionStorage.setItem('log', false)
      } else {
        // 没有
        // const hisobj = { history: [] }
        // payload.push(hisobj)
        state.use.push({
          ...payload,
        })
        sessionStorage.setItem('current-user', payload.username)
        sessionStorage.setItem('log', true)
      }
    },
    seekToUse (state, payload) {
      const has = state.use.find((prod) => { return prod.username === payload.username })
      console.log('find:' + has)
      //   console.log(has.username)
      if (has === undefined) {
        // 没有
        alert('用户名或密码错误')
        sessionStorage.setItem('log', false)
      } else {
        // 有
        if (payload.password === has.password) {
          sessionStorage.setItem('current-user', payload.username)
          sessionStorage.setItem('log', true)
        } else {
          alert('用户名或密码错误')
        }
      }
    },
    historyToUse (state, payload) {
      // console.log(1111)
      const username = sessionStorage.getItem('current-user') || ''
      if (username === '') {} else {
        const has = state.use.find((prod) => { return prod.username === username })
        const hisobj = has.history
        // console.log('hisobj === []' + hisobj === [])
        // JSON.stringify
        // console.log('payload:' + JSON.stringify(payload))
        // console.log(payload[0])
        if (JSON.stringify(payload) === '{}') {
          console.log('没有')
        } else {
          const his = hisobj.find((prod) => { return prod.id === payload.id })

          if (his) {
          } else {
            hisobj.push({
              ...payload,
            })
          }
        }
        // console.log('hisobj1:' + hisobj)
        // console.log('has：' + has)
        // console.log('hisobj：' + hisobj)
      }
    },
    historygetUse (state) {
      const username = sessionStorage.getItem('current-user') || ''
      if (username === '') {} else {
        const has = state.use.find((prod) => { return prod.username === username })
        const hisobj = has.history
        sessionStorage.setItem('history', JSON.stringify(hisobj))
      }
    },

  },
}

export default UseModule
